Use filetime for set_file_times
authorAlex Crichton <alex@alexcrichton.com>
Wed, 29 Jul 2015 17:25:22 +0000 (10:25 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Wed, 29 Jul 2015 17:25:22 +0000 (10:25 -0700)
Don't vendor the implementation locally.

Closes #1859

Cargo.lock
tests/support/paths.rs

index aeb60472a6a120f850dbf50b390d4b4d48cd1be9..1f1da6d19ec94037cd0f7ea527b2c19df61546ec 100644 (file)
@@ -8,7 +8,7 @@ dependencies = [
  "curl 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
  "docopt 0.6.69 (registry+https://github.com/rust-lang/crates.io-index)",
  "env_logger 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
- "filetime 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
+ "filetime 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
  "flate2 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
  "git2 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
  "git2-curl 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -112,8 +112,13 @@ dependencies = [
 
 [[package]]
 name = "filetime"
-version = "0.1.4"
+version = "0.1.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "kernel32-sys 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
+]
 
 [[package]]
 name = "flate2"
index 4f585645e329b08eccb2fe400689120b84b32089..57f59d712ef50fae2b1c3fb0991f4d1a0aff0db0 100644 (file)
@@ -6,7 +6,7 @@ use std::path::{Path, PathBuf};
 use std::sync::{Once, ONCE_INIT};
 use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
 
-use filetime::FileTime;
+use filetime::{self, FileTime};
 
 static CARGO_INTEGRATION_TEST_DIR : &'static str = "cit";
 static NEXT_ID: AtomicUsize = ATOMIC_USIZE_INIT;
@@ -110,72 +110,20 @@ impl CargoPathExt for Path {
             let stat = try!(path.c_metadata());
 
             let mtime = FileTime::from_last_modification_time(&stat);
-            let newtime = mtime.seconds() - 3600;
+            let newtime = mtime.seconds_relative_to_1970() - 3600;
+            let nanos = mtime.nanoseconds();
+            let newtime = FileTime::from_seconds_since_1970(newtime, nanos);
 
             // Sadly change_file_times has a failure mode where a readonly file
             // cannot have its times changed on windows.
-            match set_file_times(path, newtime, newtime) {
+            match filetime::set_file_times(path, newtime, newtime) {
                 Err(ref e) if e.kind() == io::ErrorKind::PermissionDenied => {}
                 e => return e,
             }
             let mut perms = stat.permissions();
             perms.set_readonly(false);
             try!(fs::set_permissions(path, perms));
-            set_file_times(path, newtime, newtime)
-        }
-
-        #[cfg(unix)]
-        fn set_file_times(p: &Path, atime: u64, mtime: u64) -> io::Result<()> {
-            use std::os::unix::prelude::*;
-            use std::ffi::CString;
-            use libc::{timeval, time_t, c_char, c_int};
-
-            let p = try!(CString::new(p.as_os_str().as_bytes()));
-            let atime = timeval { tv_sec: atime as time_t, tv_usec: 0, };
-            let mtime = timeval { tv_sec: mtime as time_t, tv_usec: 0, };
-            let times = [atime, mtime];
-            extern {
-                fn utimes(name: *const c_char, times: *const timeval) -> c_int;
-            }
-            unsafe {
-                if utimes(p.as_ptr(), times.as_ptr()) == 0 {
-                    Ok(())
-                } else {
-                    Err(io::Error::last_os_error())
-                }
-            }
-        }
-
-        #[cfg(windows)]
-        fn set_file_times(p: &Path, atime: u64, mtime: u64) -> io::Result<()> {
-            use std::fs::OpenOptions;
-            use std::os::windows::prelude::*;
-            use winapi::{FILETIME, DWORD};
-            use kernel32;
-
-            let f = try!(OpenOptions::new().write(true).open(p));
-            let atime = to_filetime(atime);
-            let mtime = to_filetime(mtime);
-            return unsafe {
-                let ret = kernel32::SetFileTime(f.as_raw_handle() as *mut _,
-                                                0 as *const _,
-                                                &atime, &mtime);
-                if ret != 0 {
-                    Ok(())
-                } else {
-                    Err(io::Error::last_os_error())
-                }
-            };
-
-            fn to_filetime(seconds: u64) -> FILETIME {
-                // FILETIME is a count of 100ns intervals, and there are 10^7 of
-                // these in a second
-                let seconds = seconds * 10_000_000;
-                FILETIME {
-                    dwLowDateTime: seconds as DWORD,
-                    dwHighDateTime: (seconds >> 32) as DWORD,
-                }
-            }
+            filetime::set_file_times(path, newtime, newtime)
         }
     }